package easyerp.controller;

import easyerp.entity.Product;
import easyerp.service.ProductService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/products")
public class ProductController {

    private final ProductService productService;

    // 构造函数注入（推荐）
    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    // 查询所有产品
    @GetMapping
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

    // 根据 ID 查询产品
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable UUID id) {
        return productService.getProductById(id);
    }

    // 新增产品
    @PostMapping
    public Product insertProduct(@RequestBody Product product) {
        return productService.insertProduct(product);
    }

    // 根据 ID 更新产品
    @PutMapping("/{id}")
    public Product updateProduct(@PathVariable UUID id, @RequestBody Product product) {
        return productService.updateProduct(id, product);
    }

    // 根据 ID 删除产品
    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable UUID id) {
        productService.deleteProduct(id);
    }
}
